import Vue from 'vue'
import Router from 'vue-router'
 

Vue.use(Router)  

const router = new Router({
  routes: [
    {
      path: '/index',
      name: 'HelloUser',
      component: ()=>import("@/views/home/unlogin"),
      meta:{
      	requiresAuth: false,
        title:'首页'
      }
    },
    //首页
    {
      path: '/about',
      name: 'aboutus',
      component: ()=>import("@/views/home/index"),
      meta:{
      	requiresAuth: false,
        title:'关于我们'
      }
    },
    //在售产品推荐
    {
      path: '/currentSell',
      name: 'currentSell',
      component: ()=>import("@/views/product/index"),
      meta:{
      	requiresAuth: false,
        title:'在售产品推荐'
      }
    },
    //在售产品详情
    {
      path: '/currentSell/detail',
      name: 'currentSellDetail',
      component: ()=>import("@/views/product/detail"),
      meta:{
      	requiresAuth: true,
        title:'在售产品推荐/详情' 
      }
    },
    
    //资产 推荐
    {
      path: '/property',
      name: 'property',
      component: ()=>import("@/views/property/index"),
      meta:{
        requiresAuth: false,
        title:"资产推荐" 
      }
    },
    //资产详情
    {
      path: '/property/detail',
      name: 'propertyDetail',
      component: ()=>import("@/views/property/detail"),
      meta:{
        requiresAuth: true,
        title:'资产推荐/详情' 
      }
    },
    
    //发布合作信息
    {
      path: '/release',
      name: 'release',
      component: ()=>import("@/views/release/index"),
      meta:{
        requiresAuth: false,
        title:'发布合作信息'
      }
    },
    //首页
    {
      path: '/introduce',
      name: 'introduce',
      component: ()=>import("@/views/home/company_introduce"),
      meta:{
        requiresAuth: false,
        title:'公司简介'
      }
    },
  ]
})

/*路由拦截器设置-before*/
router.beforeEach((to, from, next) => {
    // console.log(to,from) 
    let token = sessionStorage.getItem("token")
    //1.登录权限判断
    if(to.meta.requiresAuth) {
        //验证token，存在才跳转
        if(token) {
            next()
        } 
        else {
          
            next({
                path: "/index",
                query: { redirect: to.fullPath }
            })
        }
    } 
    else{
      if(to.path == '/'){
        next({path:'/index'})
       
      }
      else{
         next()
      }
    }
    /*2.非登陆条件下输入某个地址 需要先跳到登录页再跳转到相应页面*/
    // if(to.matched.length == 0 ){
    //     next({path: "/",query: { redirect: to.fullPath }})
    // }
});
router.afterEach((to,from,next) => {
  // console.log(to)
  let path = to.path.split('/').slice(1)
  // console.log(path)
  let title = to.meta.title.split('/')
  // console.log(title)
  let arr = [{path:'/index',title:'首页'}]
  if((path.length == 1) && (path[0]=='index' || path[0]=='about'  || path[0]=='introduce')){

  }
  else{
      path.forEach((item,index)=>{
        title.forEach((it,ind)=>{
          if(index == ind){
            arr.push({path:'/'+item,title:it})
          }
        })
      })
  }
  
  // console.log(arr)
  router.app.$store.commit("setbreadcrumbItems", arr);
})

export default router